package com.sinux.sshUser.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sinux.sshUser.entity.Department;
import com.sinux.sshUser.entity.Role;
import com.sinux.sshUser.entity.User;
import com.sinux.sshUser.service.DepartmentServiceI;
import com.sinux.sshUser.service.ResourcesServiceI;
import com.sinux.sshUser.service.RoleServiceI;
import com.sinux.sshUser.service.UserServiceI;

@Controller
public class TabController {
	
	@Autowired
	private UserServiceI userService;
	@Autowired
	private DepartmentServiceI departService;
	@Autowired
	private RoleServiceI roleService;
	@Autowired
	private ResourcesServiceI resService;
	
	@RequestMapping("/tabdata")
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		
		return "include/tab";
	}
	
	/**修改用户部门关联
	 * 1.根据uid获取该用户的部门集合,
	 * 2.根据deid获取部门,
	 * 3.将部门添加到集合中
	 */
	@ResponseBody
	@RequestMapping("/inude")
	public Map<String, Object> inude(HttpServletRequest request, HttpServletResponse response, Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String[]> params=request.getParameterMap();
		String[] departids=params.get("deids");//["0,1,2,..."] 长度为1
		//System.out.println("选中的部门编号:"+departids[0]);
		String ss=departids[0];
		
		String[] deids= ss.split(",");//["0","1","2",...]长度为n
		String[] uid=params.get("uid");
		boolean insTAG=userService.instude(uid[0], deids);
		if(insTAG){
			map.put("success", true);
			map.put("msg", "成功");
		}else{
			map.put("success", false);
			map.put("msg", "失败");
		}
		return map;
	} 
	//查询用户部门关联
	@ResponseBody
	@RequestMapping("/selude")
	public Map<String, Object> selude(HttpServletRequest request, HttpServletResponse response, Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		String uid=request.getParameter("uid");
		User user=userService.get(uid);
		Set<Department> departs=user.getDeparts();
		Iterator<Department> it = departs.iterator();
		List list=new ArrayList();
		if(departs.size()==0){
			map.put("success", false);
			map.put("msg", "查询失败或无关联");
		}else{
			while(it.hasNext()){//判断是否有下一个
				Department depart=it.next();//取出元素
				list.add(depart.getId());
			}
			map.put("success", true);
			map.put("msg", "查询成功");
			map.put("data", list);
		}
		
		return map;
	} 
	//删除用户部门关联
	@ResponseBody
	@RequestMapping("/delude")
	public Map<String, Object> delude(HttpServletRequest request, HttpServletResponse response, Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		String uid=request.getParameter("uid");
		String deid=request.getParameter("deid");
		boolean delTAG=userService.delstude(uid, deid);
		if(delTAG){
			map.put("success", true);
			map.put("msg", "成功");
		}else{
			map.put("success", false);
			map.put("msg", "失败");
		}
		return map;	
	}

	/**
	 * 用户角色关联
	 * 
	 */
	//查询用户角色关联
	@ResponseBody
	@RequestMapping("/seluro")
	public Map<String,Object> seluro(HttpServletRequest request,HttpServletResponse response,Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		String uid=request.getParameter("uid");
		System.out.println("查询角色获取的uid:"+uid);
		User user=userService.get(uid);
		Set<Role> roles=user.getRoles();
		Iterator<Role> it = roles.iterator();
		List list=new ArrayList();
		if(roles.size()==0){
			map.put("success", false);
			map.put("msg", "未分配角色");
		}else{
			while(it.hasNext()){//判断是否有下一个
				Role role=it.next();//取出元素
				list.add(role.getRoid());
			}
			map.put("success", true);
			map.put("msg", "查询成功");
			map.put("data", list);
		}
		return map;
	}
	
	//修改用户角色关联
	@ResponseBody
	@RequestMapping("/insuro")
	public Map<String,Object> inuro(HttpServletRequest request,HttpServletResponse response,Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String[]> params=request.getParameterMap();
		String[] arr=params.get("roids");//["0,1,2,..."] 长度为1
		System.out.println("选中的角色ID:"+arr[0]);
		String ss=arr[0];
		String[] roIds= ss.split(",");//["0","1","2",...]长度为n
		String[] uId=params.get("uid");
		System.out.println("选中的用户ID:"+uId[0]);
		boolean insTAG=userService.updateUserRo(uId[0], roIds);
		if(insTAG){
			map.put("success", true);
			map.put("msg", "成功");
		}else{
			map.put("success", false);
			map.put("msg", "失败");
		}
		return map;
		
	}
	
}

